package gui.finalsOptions;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.LayoutStyle;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

import core.CustomIcons;
import core.GuiComponentLabels;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class FinalsOptionsWindow extends javax.swing.JFrame {
	private static FinalsOptionsWindow instance = null;
	private JTable finalsPermissionTable;
	private JScrollPane jScrollPane1;
	private JButton closeButton;
	private JButton saveButton;

	private final FinalsOptionsController controller;

	/**
	 * Auto-generated main method to display this JFrame
	 */
	public static void main(String [] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				FinalsOptionsWindow.getInstance().setLocationRelativeTo(null);
				FinalsOptionsWindow.getInstance().setVisible(true);
			}
		});
	}

	private FinalsOptionsWindow() {
		super();
		this.setIconImage(CustomIcons.iconImage);
		this.controller = new FinalsOptionsController(this);
		this.initGUI();
		this.controller.buildTable();
	}

	public static FinalsOptionsWindow getInstance() {
		if (instance == null) {
			instance = new FinalsOptionsWindow();
		}
		return instance;
	}

	private void initGUI() {
		try {
			GroupLayout thisLayout = new GroupLayout(this.getContentPane());
			this.getContentPane().setLayout(thisLayout);
			thisLayout.setVerticalGroup(thisLayout
					.createSequentialGroup()
					.addContainerGap()
					.addComponent(this.getJScrollPane1(), 0, 222, Short.MAX_VALUE)
					.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED, 1, GroupLayout.PREFERRED_SIZE)
					.addGroup(
							thisLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
									.addComponent(this.getSaveButton(), GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
									.addComponent(this.getCloseButton(), GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
					.addContainerGap());
			thisLayout.setHorizontalGroup(thisLayout
					.createSequentialGroup()
					.addContainerGap()
					.addGroup(
							thisLayout
									.createParallelGroup()
									.addComponent(this.getJScrollPane1(), GroupLayout.Alignment.LEADING, 0, 533, Short.MAX_VALUE)
									.addGroup(
											GroupLayout.Alignment.LEADING,
											thisLayout.createSequentialGroup().addGap(0, 370, Short.MAX_VALUE)
													.addComponent(this.getSaveButton(), GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
													.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
													.addComponent(this.getCloseButton(), GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))).addContainerGap());
			this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			this.pack();
			this.setSize(567, 309);
		} catch (Exception e) {
			// add your error handling code here
			e.printStackTrace();
		}
	}

	private JTable getFinalsPermissionTable() {
		if (this.finalsPermissionTable == null) {
			this.finalsPermissionTable = new JTable();
			this.finalsPermissionTable.setDefaultRenderer(Object.class, new FinalsOptionsTableModelRenderer());
			this.finalsPermissionTable.setAutoCreateRowSorter(true);
		}
		return this.finalsPermissionTable;
	}

	private JButton getSaveButton() {
		if (this.saveButton == null) {
			this.saveButton = new JButton();
			this.saveButton.setText(GuiComponentLabels.SAVE_BUTTON);
			this.saveButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent evt) {
					FinalsOptionsWindow.this.controller.savePermissions();
				}
			});
		}
		return this.saveButton;
	}

	private JButton getCloseButton() {
		if (this.closeButton == null) {
			this.closeButton = new JButton();
			this.closeButton.setText(GuiComponentLabels.CLOSE_BUTTON);
			this.closeButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent evt) {
					FinalsOptionsWindow.this.controller.closeWindow();
				}
			});
		}
		return this.closeButton;
	}

	public JTable getFinalsPermissionsTable() {
		return this.finalsPermissionTable;
	}

	public FinalsOptionsController getController() {
		return this.controller;
	}

	private JScrollPane getJScrollPane1() {
		if (this.jScrollPane1 == null) {
			this.jScrollPane1 = new JScrollPane();
			this.jScrollPane1.setViewportView(this.getFinalsPermissionTable());
		}
		return this.jScrollPane1;
	}
}
